<?php
namespace apps\fzq\controllers;

use enums\QueueEnums;

if( ! APP_ROOT  ) return 'Direct Access Deny!';

class RedisController extends \Phalcon\Mvc\Controller
{

	/**
	 * @comment( value = '' )
	 */
	public function initialize( )
	{
	}

	public function tpAction()
	{
		$redis = $this->di->get( 'redisCache' );
		
// 		var_dump( get_class_methods( get_class( $redis )));
// 		$redis->save( 'redisCache', 3, -1 );
	}
	
	public function tnAction()
	{
		$nredis = $this->di->get( 'nredis' );
	}
	/**
	 * 秒杀和抢购在前端可以使用同一算法 不同的是二者在后端业务的处理方式不同
	 *
	 * 大致算法为：先设置要秒杀（抢购）商品的数量
	 * 然后每次购买时递减商品数量在每次秒杀（抢购）时设置当前用户的信息比如排序信息等
	 *
	 */
	public function sk2Action( )
	{
		$iIndex = $this->nredis->incrBy( 'sk2_index', -1 );
		if( $iIndex < 0 )
		{
			// send static html file
			echo '秒杀（抢购）结束！敬请关注其他产品！<br>';
			return;
		}
	
		if( $this->nredis->rpush( 'seckill2', $iIndex, 'bruce' . $iIndex ) )
		{
			echo '恭喜！您已秒杀（抢购）成功！<br>';
		}
		else
		{
			echo '错误，请刷新页面重试!<br>';
		}
	}
	
	/**
	 * 秒杀和抢购在前端可以使用同一算法 不同的是二者在后端业务的处理方式不同
	 * 
	 * 大致算法为：先设置要秒杀（抢购）商品的数量
	 * 然后每次购买时递减商品数量在每次秒杀（抢购）时设置当前用户的信息比如排序信息等
	 * 
	 */
	public function seckillAction( )
	{
		$iIndex = $this->nredis->incrBy( 'seckill_index', -1 );
		if( $iIndex < 0 )
		{
			// send static html file
			echo '秒杀（抢购）结束！敬请关注其他产品！<br>';
			return;
		}
		
		if( $this->nredis->zAdd( 'shoot', $iIndex, 'bruce' . $iIndex ) )
		{
			echo '恭喜！您已秒杀（抢购）成功！<br>';
		}
		else
		{
			echo '错误，请刷新页面重试!<br>';
		}
	}

	public function iAction( )
	{
		for( $i = 0; $i < 1000; ++$i )
		{
			$arrParams = $this->rqueue->getMsgProto( 'sn: ' . $i . ' This is a short message from bruce!', QueueEnums::BIZ_SMS, 'Sms', 'activate', $id = false, $priority = QueueEnums::PRIORITY_MEDIUM, $msgt = QueueEnums::MSGTYPE_STRING );
			
			var_dump( $this->rqueue->sendMsg( $arrParams ) );
		}
	}

	public function phqlAction( )
	{
		
		// $r = $this->modelsManager->executeQuery( 'select tb1.id, tb1.name, tb2.address from apps\fzq\models\TB1 as tb1 inner join apps\fzq\models\TB2 tb2 on tb1.id = tb2.tid' );
		$r = $this->modelsManager->createBuilder()->columns( 'tb1.id, tb2.address' )->from( [ 'tb1' => 'apps\fzq\models\TB1' 
		] )->join( 'apps\fzq\models\TB2', 'tb1.id = tb2.tid', 'tb2', 'INNER' )->getQuery()->execute();
		
		foreach( $r as $v )
		{
			var_dump( $v->toArray() );
		}
	}

	public function sAction()
	{
		$redis = $this->di->get( 'nredis');
		$redis->set( 'aaaa', 'AAAAAAAAA' );	
	}
	
	
	public function gAction()
	{
		echo __FILE__, __LINE__ , '<br>';
		$redis = $this->di->get( 'nredis' );
		while( true )
		{
			$a = $redis->get( 'aaaa' );
			echo $a, '<br>';
			break;
		}	
	}
	
	public function pubAction( )
	{
		$redis = new \Redis();
		$redis->connect( '127.0.0.1', '6379' );
		var_dump( $redis->publish( 'chan-1', 'hello' ) );
	}

	public function subAction( )
	{
		// $this->nredis->subscribe( array( 'Orders', 'chan-b' ), function( $redis, $chan, $msg ){
		// echo 'chanel:' . $chan . ' msg: ' . $msg . '<br>';
		// });
		$redis = new \Redis();
		$redis->pconnect( '127.0.0.1', '6379' );
		
		$redis->subscribe( array( 'chan-1' 
		), function ( $r, $chan, $msg )
		{
			switch( $chan )
			{
				case 'chan-1':
					break;
				
				case 'chan-2':
					break;
				
				case 'chan-2':
					break;
			}
		} );
	}

	private function subA( )
	{
	}

	/**
	 * @comment( value = '' )
	 */
	public function indexAction( )
	{
		$strVal = 'hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!';
		// for( $i = 0; $i < 1000000; ++$i )
		// {
		// CommUtils::UUIDNoDash();
		// // $this->rqueue->lpush( 'queue1', $strVal );
		// }
		$time = microtime( true );
		
		for( $i = 0; $i < 100000; ++$i )
		{
			$this->rqueue->sendMsg( $strVal );
		}
		var_dump( microtime( true ) - $time );
		
		// for( $i = 0; $i < 10; ++$i )
		// {
		// var_dump( unserialize( $this->rqueue->rpop( 'queue1' ) ));
		// }
		
		// $this->rqueue->rpoplpush( '' );
	}

	public function popAction( )
	{
		// var_dump( $this->rqueue->sendMsg( 'abc' ));
		$a = $this->rqueue->getMsg();
		
		var_dump( $a );
		
		var_dump( $this->rqueue->delete( $a ) );
		// for( $i = 0; $i < 10000; ++$i )
		// {
		// $a = $this->rqueue->getMsg();
		// echo $a;
		// }
		
		// for( $i = 0; $i < 1000; ++$i )
		// {
		// $this->nredis->select( 15 );
		// var_dump( $this->nredis->lpop('bakqueue'));
		// }
		// var_dump( $this->rqueue->( $a ) );
		
		// var_dump( $this->nredis->lpush( 'q', 'qqqq' ));
		
		// $a = var_dump( $this->nredis->rpoplpush( 'q', 'bq' ));
		
		// echo $a;
		
		// var_dump( $this->nredis->rpop( 'q' ));
		// for( $i = 0; $i < 10; ++$i )
		{
			// var_dump( $this->rqueue->getServer());
			// var_dump( $this->rqueue->getMsg());
		}
	}
}